﻿/*
	Copyright © Bogdan Kobets 2011
	http://ElasticLogic.com
*/

using System.Collections.Generic;
using ElasticLogic.TagsMarker.Concepts;

namespace ElasticLogic.TagsMarker.Collections
{

	public sealed class CategoriesCollection<E> : LabelsCollection<Category<E>>
		where E : class
	{
		public CategoryCreator<E> Creator { get; set; }

		public CategoriesCollection()
			: base()
		{
		}

		public List<Category<E>> Filled
		{
			get { return Marker<E>.GetUsingCategories(true); }
		}

		public List<Category<E>> Empty
		{
			get { return Marker<E>.GetUsingCategories(false); }
		}

		public bool Add(string caption)
		{
			if (Creator == null)
				return false;

			Category<E> category = Creator(caption);

			if (category != null)
				return base.Add(category);
			else
				return false;
		}

		public bool Remove(string caption)
		{
			return this.Remove(base[caption]);
		}

		public bool Remove(Category<E> category)
		{
			if (base.Contains(category))
			{
				Marker<E>.ClearCategoryTags(category);
				return base.RemoveItem(category);
			}
			else
				return false;
		}

		public void Clear()
		{
			foreach (Category<E> category in base.Items)
			{
				this.Remove(category);
			}
		}

		public Category<E> ByTag(Tag<E> tag)
		{
			return Marker<E>.GetTagCategory(tag);
		}

	}

	public delegate Category<E> CategoryCreator<E>(string caption)
		where E: class;

}
